package com.leased.controller;

import com.leased.enums.BusinessStatusEnum;
import com.leased.exception.YCException;
import com.leased.handler.AdvertInfoHandler;
import com.leased.pojo.AdvertInfo;
import com.leased.request.BaseQuery;
import com.leased.request.advert.AdvertInfoRequest;
import com.leased.utils.APIResponse;
import com.leased.utils.Page;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by shencx on 2017/8/9.
 */
@Controller
@RequestMapping(value = "/api/advert")
public class AdvertInfoController {

    final Logger LOG = LoggerFactory.getLogger(AdvertInfoController.class);

    @Resource
    private AdvertInfoHandler advertInfoHandler;

    /**
     * 获取广告列表
     * @param request
     * @param baseQuery
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public APIResponse<Page<AdvertInfo>> list(HttpServletRequest request, BaseQuery baseQuery){
        APIResponse<Page<AdvertInfo>> apiResponse = new APIResponse<>();
        Page<AdvertInfo> page = null;
        try {
            page = advertInfoHandler.getAdvertInfoPage(baseQuery);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(page);
        } catch (Throwable e) {
            LOG.error("获取广告信息列表发生异常", baseQuery);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 添加修改广告
     * @param request
     * @param advertInfoRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editor",method = RequestMethod.POST)
    public APIResponse editor(HttpServletRequest request, AdvertInfoRequest advertInfoRequest){
        APIResponse apiResponse = new APIResponse();
        try {
            advertInfoHandler.addAdvertInfo(advertInfoRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("添加修改广告信息发生异常", advertInfoRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取广告详情
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public APIResponse<AdvertInfo> list(HttpServletRequest request, Integer id){
        APIResponse<AdvertInfo> apiResponse = new APIResponse<>();
        AdvertInfo advertInfo = null;
        try {
            advertInfo = advertInfoHandler.getAdvertDetail(id);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(advertInfo);
        } catch (Throwable e) {
            LOG.error("获取广告详情信息发生异常", id);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 启用禁用广告
     * @param request
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modify_status",method = RequestMethod.POST)
    public APIResponse modifyStatus(HttpServletRequest request,Integer id,Integer status){
        APIResponse apiResponse = new APIResponse();
        try {
            advertInfoHandler.modifyStatus(id,status);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("广告启用禁用发生异常", id);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }
}
